package enumdemo;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 74646
 * Date: 2022-03-30
 * Time: 19:21
 */
public enum TestEnum {

    RED("red",1),BLACK("black",2),WHITE("white",3),GREEN("green",4);
    private String name;
    private int key;
    /**
     * 1、当枚举对象有参数后，需要提供相应的构造函数
     * 2、枚举的构造函数默认是私有的 这个一定要记住
     * @param name
     * @param key
     */
    private TestEnum (String name,int key) {
        this.name = name;
        this.key = key;
    }
    public static TestEnum getEnumKey (int key) {
        for (TestEnum t: TestEnum.values()) {
            if(t.key == key) {
                return t;
            }
        }
        return null;
    }
    public static void main(String[] args) {
        System.out.println(getEnumKey(2));
    }


    public static void main4(String[] args) {
        System.out.println(RED.compareTo(GREEN));
        System.out.println(BLACK.compareTo(RED));
    }

    public static void main3(String[] args) {
        //将字符串变成对应的枚举对象
        TestEnum testEnum = TestEnum.valueOf("RED");
//        TestEnum testEnum1 = TestEnum.valueOf("BLUE"); //error
    }

    public static void main2(String[] args) {
        TestEnum[] testEnums = TestEnum.values();
        for (int i = 0; i < testEnums.length; i++) {
            System.out.println(testEnums[i]+"->"+testEnums[i].ordinal());
//            System.out.println(testEnums[i]);
        }
    }

    public static void main1(String[] args) {
            TestEnum testEnum2 = TestEnum.BLACK;
            switch (testEnum2) {
                case RED:
                    System.out.println("red");
                    break;
                case BLACK:
                    System.out.println("black");
                    break;
                case WHITE:
                    System.out.println("white");
                    break;
                case GREEN:
                    System.out.println("green");
                    break;
                default:
                    break;
            }
    }

}
